﻿using System.Collections.Generic;
using System.Linq;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Steps;
using PSE.Deployment.SetupBase.Parameter.Enums;
using PSE.Deployment.Util;
using PSE.Deployment.SetupBase.Parameter;
using PSE.Deployment.SetupBase.Configuration.Enums;

namespace PSE.Deployment.SetupBase.Groups
{
    public class SetupUpdate : SetupExecutionGroup
    {
        public SetupUpdate(IStepGroup parent)
            : base(parent)
        {
            this.Id = "SetupUpdate";
        }

        protected override void AddStepsExecution()
        {
            base.AddStepsExecution();

            List<Group> installedGroups = SetupMainFactory.GetInstance().ClonedConfiguration.SystemInfo.Groups.
                SelectDeep(g => g.SubGroups).SelectDeep(g => g.Dependencies).Where(g => (g.Install == null || g.Install.GetValueOrDefault()) && g.Installed).Distinct(new GroupComparer()).ToList();

            foreach (Group group in SetupMainFactory.GetInstance().Parameter.SetupConfiguration.SystemInfo.Groups)
            {
                if (installedGroups.Find(g => g.Id == group.Id) != null)
                {
                    this.Steps.Add(new SetupGroup(this, group, Parameter.Enums.SetupMode.Install));
                }
            }
        }
    }
}
